/**
 * 
 */
package com.haizhi.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.haizhi.bean.TbUser;
import com.haizhi.service.UserService;
import com.haizhi.user.controller.UserController;
import com.haizhi.util.MyAES;


/**
 * @author jiangjintai
 *
 */
public class UserLoginInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	UserService userService;
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String token = request.getHeader("Authorization");
		if(token==null||token.equals("")){
			TbUser tbUser = userService.findById(4);//自动注入江锦泰用户
			request.setAttribute("tbUser", tbUser);
			return super.preHandle(request, response, handler);
		}else{
			String userId = MyAES.decrypt(token, UserController.token_password);
			TbUser tbUser = userService.findById(Integer.valueOf(userId));
			request.setAttribute("tbUser", tbUser);
			return super.preHandle(request, response, handler);
		}

		
	}
	
}
